<?php
class ModelSurveyQuestion extends Model {
	public function getOptions() {
		$all_options = array();
		
		$all_options[] = array(
			'type' => 'radio',
			'name' => 'Radio'
		);
		
		$all_options[] = array(
			'type' => 'checkbox',
			'name' => 'Checkbox'
		);
		
		$all_options[] = array(
			'type' => 'select',
			'name' => 'Drop Down'
		);
		
		$all_options[] = array(
			'type' => 'text',
			'name' => 'Text'
		);
		
		$all_options[] = array(
			'type' => 'textarea',
			'name' => 'Textarea'
		);
		
		$all_options[] = array(
			'type' => 'lable',
			'name' => 'Label'
		);
		
		return $all_options;
	}
	
	public function getFormats() {
		$all_options[] = array(
			'type' => 'h1',
			'name' => 'Level 1'
		);
		
		$all_options[] = array(
			'type' => 'h2',
			'name' => 'Level 2'
		);
		
		$all_options[] = array(
			'type' => 'h3',
			'name' => 'Level 3'
		);
		
		$all_options[] = array(
			'type' => 'normal',
			'name' => 'Normal'
		);
		
		return $all_options;
	}
	
	public function addQuestion($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "question SET survey_id = '" . (int)$data['survey_id'] . "', qnum = '" . $this->db->escape($data['qnum']) . "', qtype = '" . $this->db->escape($data['qtype']) . "', qformat = '" . $this->db->escape($data['qformat']) . "', required = '" . (int)$data['question_required'] . "', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "'");

		$question_id = $this->db->getLastId(); 
			
		foreach ($data['question_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "question_description SET question_id = '" . (int)$question_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', lable = '" . $this->db->escape($value['lable']) . "', value = '" . $this->db->escape($value['value']) . "', `default` = '" . $this->db->escape($value['default']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		if (isset($data['question_store'])) {
			foreach ($data['question_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "question_to_store SET question_id = '" . (int)$question_id . "', store_id = '" . (int)$store_id . "'");
			}
		}

		if (isset($data['question_layout'])) {
			foreach ($data['question_layout'] as $store_id => $layout) {
				if ($layout) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "question_to_layout SET question_id = '" . (int)$question_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout['layout_id'] . "'");
				}
			}
		}
				
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'question_id=" . (int)$question_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		$this->cache->delete('question');
	}
	
	public function editQuestion($question_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "question SET survey_id = '" . (int)$data['survey_id'] . "', qnum = '" . $this->db->escape($data['qnum']) . "', qtype = '" . $this->db->escape($data['qtype']) . "', qformat = '" . $this->db->escape($data['qformat']) . "', required = '" . (int)$data['question_required'] . "', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "' WHERE question_id = '" . (int)$question_id . "'");

		$this->db->query("DELETE FROM " . DB_PREFIX . "question_description WHERE question_id = '" . (int)$question_id . "'");
					
		foreach ($data['question_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "question_description SET question_id = '" . (int)$question_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', lable = '" . $this->db->escape($value['lable']) . "', value = '" . $this->db->escape($value['value']) . "', `default` = '" . $this->db->escape($value['default']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "question_to_store WHERE question_id = '" . (int)$question_id . "'");
		
		if (isset($data['question_store'])) {
			foreach ($data['question_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "question_to_store SET question_id = '" . (int)$question_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "question_to_layout WHERE question_id = '" . (int)$question_id . "'");

		if (isset($data['question_layout'])) {
			foreach ($data['question_layout'] as $store_id => $layout) {
				if ($layout['layout_id']) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "question_to_layout SET question_id = '" . (int)$question_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout['layout_id'] . "'");
				}
			}
		}
				
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'question_id=" . (int)$question_id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'question_id=" . (int)$question_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		$this->cache->delete('question');
	}
	
	public function deleteQuestion($question_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "question WHERE question_id = '" . (int)$question_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "question_description WHERE question_id = '" . (int)$question_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "question_to_store WHERE question_id = '" . (int)$question_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "question_to_layout WHERE question_id = '" . (int)$question_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'question_id=" . (int)$question_id . "'");

		$this->cache->delete('question');
	}	
	
	public function getNextSortOrder($survey_id) {
		$sql = "SELECT max(sort_order) as sort_order FROM " . DB_PREFIX . "question WHERE status=1 AND survey_id = '" . (int)$survey_id . "'";
//		echo $sql; 
		$query = $this->db->query($sql);
		return $query->row['sort_order'] + 1; 
	}

	public function getQuestion($question_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'question_id=" . (int)$question_id . "') AS keyword FROM " . DB_PREFIX . "question WHERE question_id = '" . (int)$question_id . "'");
		
		return $query->row;
	}
		
	public function getQuestions($data = array()) {
		if ($data) {
			$sql = "SELECT i.*, id.*, sd.title as survey  FROM " . DB_PREFIX . "question i LEFT JOIN (" . DB_PREFIX . "question_description id, " . DB_PREFIX . "survey_description sd) ON (i.question_id = id.question_id AND i.survey_id = sd.survey_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' AND sd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
			
			if (isset($data['filter_survey_id']) && !is_null($data['filter_survey_id'])) {
				$sql .= " AND i.survey_id = '" . (int)$data['filter_survey_id'] . "'";
			}
		
			$sort_data = array(
				'i.question_id',
				'i.survey_id',
				'id.title',
				'id.title',
				'i.sort_order'
			);		
		
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY i.survey_id desc, i.qnum, i.question_id ";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= "";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}		

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
//			echo $sql;
			
			$query = $this->db->query($sql);
			
			return $query->rows;
		} else {
			$question_data = $this->cache->get('question.' . $this->config->get('config_language_id'));
		
			if (!$question_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "question i LEFT JOIN " . DB_PREFIX . "question_description id ON (i.question_id = id.question_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY id.title");
	
				$question_data = $query->rows;
			
				$this->cache->set('question.' . $this->config->get('config_language_id'), $question_data);
			}	
	
			return $question_data;			
		}
	}
	
	public function getQuestionDescriptions($question_id) {
		$question_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "question_description WHERE question_id = '" . (int)$question_id . "'");

		foreach ($query->rows as $result) {
			$question_description_data[$result['language_id']] = array(
				'title'       => $result['title'],
				'lable' => $result['lable'],
				'value' => $result['value'],
				'default' => $result['default'],
				'description' => $result['description']
			);
		}
		
		return $question_description_data;
	}
	
	public function getQuestionStores($question_id) {
		$question_store_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "question_to_store WHERE question_id = '" . (int)$question_id . "'");

		foreach ($query->rows as $result) {
			$question_store_data[] = $result['store_id'];
		}
		
		return $question_store_data;
	}

	public function getQuestionLayouts($question_id) {
		$question_layout_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "question_to_layout WHERE question_id = '" . (int)$question_id . "'");
		
		foreach ($query->rows as $result) {
			$question_layout_data[$result['store_id']] = $result['layout_id'];
		}
		
		return $question_layout_data;
	}
		
	public function getTotalQuestions($data = array()) {
		$sql = "SELECT  COUNT(i.question_id) AS total FROM " . DB_PREFIX . "question i LEFT JOIN (" . DB_PREFIX . "question_description id, " . DB_PREFIX . "survey_description sd) ON (i.question_id = id.question_id AND i.survey_id = sd.survey_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' AND sd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		
		if (isset($data['filter_survey_id']) && !is_null($data['filter_survey_id'])) {
			$sql .= " AND i.survey_id = '" . (int)$data['filter_survey_id'] . "'";
		}
		
		$query = $this->db->query($sql);
			
		return $query->row['total'];
	}	
	
	public function getTotalQuestionsByLayoutId($layout_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "question_to_layout WHERE layout_id = '" . (int)$layout_id . "'");

		return $query->row['total'];
	}	
}
?>